package com.rachel.materialdemo.adapter;

import android.content.Context;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rachel.materialdemo.ItemClickListener;
import com.rachel.materialdemo.R;
import com.rachel.materialdemo.ToolUtils;
import com.rachel.materialdemo.bean.Cardone;
import com.rachel.materialdemo.bean.Cardtwo;
import com.rachel.materialdemo.bean.ViewBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.rachel.materialdemo.ToolUtils.TYPE_ONE;
import static com.rachel.materialdemo.ToolUtils.TYPE_TWO;
import static com.rachel.materialdemo.ToolUtils.TYPE_VIEWPAGER;

/**
 * Created by zhengshaorui on 2017/8/21.
 */

public class DemoRecyclerviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    Context mContext;


    private List<Integer> mTypeList = new ArrayList<>();
    private Map<Integer,Integer> mTypeMap = new HashMap<>();

    private List<Cardone> mCardones;
    private List<Cardtwo> mCardtwos;
    private List<ViewBean> mViewBeen;
    private View mHeadView;
    private SparseArrayCompat<View> mHeaderViews =  new SparseArrayCompat<>();

    private ItemClickListener mItemClickListener;
    public void setonItemClickListener(ItemClickListener listener){

        mItemClickListener = listener;
    }

    public void addHeader(View view){
        // add 的情况下，我们是可以多加的
        mHeaderViews.put(ToolUtils.TYPE_HEARDER + mHeaderViews.size(),view);
    }

    public void setDatas(List<ViewBean> viewImgs, List<Cardone> cardones, List<Cardtwo> cardtwos){
        mTypeList.clear();
        mTypeMap.clear();

        addtype(ToolUtils.TYPE_VIEWPAGER,viewImgs);
        addtype(ToolUtils.TYPE_ONE,cardones);
        addtype(ToolUtils.TYPE_TWO,cardtwos);
        mCardones = cardones;
        mCardtwos = cardtwos;
        mViewBeen = viewImgs;
    }

    private void addtype(int type,List list){
        mTypeMap.put(type,mTypeList.size());
        for (int i = 0; i < list.size(); i++) {
            mTypeList.add(type);

        }
    }





    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null){
            mContext = parent.getContext();
        }
        switch (viewType){
            case TYPE_VIEWPAGER:
                return new ViewPagerViewholder(mContext,LayoutInflater.from(mContext)
                        .inflate(R.layout.viewpager_layout,parent,false));

            case TYPE_ONE:
                return new CardOneViewholder(LayoutInflater.from(mContext)
                        .inflate(R.layout.cardview_item,parent,false));

            case TYPE_TWO:
                return new CardTwoViewholder(LayoutInflater.from(mContext)
                        .inflate(R.layout.cardview_item2,parent,false));
        }

        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int viewtype = getItemViewType(position);
        int realpostion = position - mTypeMap.get(viewtype);
        switch (viewtype){
            case TYPE_VIEWPAGER:
                //这里不用做什么，直接在viewpagerviewholder 里面做数据处理了
                break;
            case TYPE_ONE:
                CardOneViewholder oneholder = (CardOneViewholder) holder;
                oneholder.onBindView(mCardones.get(realpostion),position,mItemClickListener);
                break;
            case TYPE_TWO:
                CardTwoViewholder twoholder = (CardTwoViewholder) holder;
                twoholder.onBindView(mCardtwos.get(realpostion),position,mItemClickListener);
                break;
        }
    }

    private int getHeaderCount(){
        return mHeaderViews.size();
    }


    @Override
    public int getItemCount() {
        return mTypeList.size() +getHeaderCount();
    }



    @Override
    public int getItemViewType(int position) {
        if (mHeaderViews.keyAt(position) > 0){

        }
        return mTypeList.get(position);
    }
}
